<?php

namespace App\Http\Controllers\App;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Wares;
/**
 * 队列商品控制器
 */
// class WaresController extends Controller
class WaresController extends BaseController
{
    /**
     * 商品列表(GET) api/wares/wareslist
     * 
     * @return json
     */
	public function waresList(Request $request)
    {
		// 商户SESSION数据
        // session(['shopInfo'=>['id'=>1]]); 
		$shopInfo = $request->session()->get('shopInfo');

        // 商户状态判断
        $shop_id = $shopInfo['id'];     // 店铺id

        if(!$shop_id){

            return self::error('未登录');

        }

		// 初始化数组
		$where = $return = array();

		// 构造查询条件
		$where['shop_id'] 	= $shop_id;
		$where['delete']	= 1;

		// 查询列表
		$return = Wares::where($where)->orderBy('displayorder')->paginate(10);

		return self::json($return , '查询成功');
		
	}

    /**
     * 商品详情(GET) api/wares/waresinfo
     *
     * @return json
     */
    public function waresinfo(Request $request)
    {
        // 商户SESSION数据
        $shopInfo = $request->session()->get('shopInfo');

        // 商户状态判断
        $shop_id = $shopInfo['id'];     // 店铺id
        // $shop_id = 1;

        if(!$shop_id){
            return self::error('未登录');
        }

        $id = $request->input('id');
        if(!$id){
            return self::error('参数有误');
        }

        $where['shop_id']   = $shop_id;
        $where['id']        = $id;
        $where['delete']    = 1;

        $return = Wares::where($where)->first();

        if($return){
            return self::json($return , '查询成功');
        }else{
            return self::error('数据为空');
        }

    }
    
    /**
     * 商品添加(POST) api/wares/addware
     * @param int $name 		商品名称
     * @param int $logo 		LOGO
     * @param int $cost_price	成本价
     * @param int $sell_price 	销售价
     * @param int $stock 		库存
     * @param int $unit 		计量单位
     * @param int $introduce	商品描述
     * @return json
     */
    public function addWare(Request $request)
    {
        // 商户SESSION数据
        $shopInfo = $request->session()->get('shopInfo');

        // 商户状态判断
        $shop_id = $shopInfo['id'];     // 店铺id

        if(!$shop_id){

            return self::error('未登录');

        }

    	// 数据整理
    	$name			= $request->input('name', '');              // 商品
    	$logo			= $request->input('logo', '');              // logo
        $pics           = $request->input('pics', '');              // 商品图片
    	$bar_code		= $request->input('bar_code', '');          // 国际条形码
    	$cost_price		= $request->input('cost_price', '0');		// 成本价--已分为单位
    	$sell_price		= $request->input('sell_price', '0');		// 销售价--已分为单位
    	$spec			= $request->input('spec', '');				// 规格
    	$stock			= $request->input('stock', 0);				// 库存
    	$unit			= $request->input('unit', '');				// 计量单位
    	$introduce		= $request->input('introduce', '<p></p>');  // 商品描述

    	// 必填项判断
    	if(empty($name) || empty($logo) || $cost_price<=0 || $sell_price<=0 || empty($spec) || empty($unit)){

    		return self::error('必填项信息不完整');

    	}

        // 获取商品排序最大值
        $max = Wares::where('shop_id',$shop_id)->max('displayorder');

    	// 实例化model
    	$wares = new Wares;

    	$wares->name		= $name;
        $wares->logo		= $logo;
        $wares->pics        = $pics;
        $wares->bar_code    = $bar_code;
        $wares->cost_price	= $cost_price;
        $wares->sell_price	= $sell_price;
        $wares->spec		= $spec;
        $wares->stock 		= $stock;
        $wares->unit		= $unit;
        $wares->introduce	= $introduce;
        $wares->shop_id     = $shop_id;
        $wares->displayorder= $max+1;

        // 数据入表
        $re = $wares->save();

        if($re){

        	return self::success('添加成功');

        }else{

        	return self::error('添加失败');

        }	

    }
    
    /**
     * 商品编辑(POST) api/wares/upware
     * @param int $id 			商品id
     * @param int $name 		商品名称
     * @param int $logo 		LOGO
     * @param int $cost_price	成本价
     * @param int $sell_price 	销售价
     * @param int $stock 		库存
     * @param int $unit 		计量单位
     * @param int $introduce	商品描述
     * @return json
     */
    public function upWare(Request $request)
    {
        // 商户SESSION数据
        $shopInfo = $request->session()->get('shopInfo');

        // 商户状态判断
        $shop_id = $shopInfo['id'];     // 店铺id

        if(!$shop_id){

            return self::error('未登录');

        }

    	$id = $request->input('id');

    	if(!$id){

    		return self::error('缺少参数');

    	}

    	// 初始化数组
    	$data = array();

    	// 数据整理
    	$request->input('name', '') 		== null || $data['name'] 		= $request->input('name');				// 商品
    	$request->input('logo', '') 		== null || $data['logo'] 		= $request->input('logo');				// logo
        $request->input('pics', '')         == null || $data['pics']        = $request->input('pics');              // 商品图片
    	$request->input('bar_code', '')		== null || $data['bar_code']	= $request->input('bar_code');			// 国际条形码
    	$request->input('cost_price') 		== null || $data['cost_price'] 	= $request->input('cost_price');		// 成本价--已分为单位
    	$request->input('sell_price') 		== null || $data['sell_price']	= $request->input('sell_price');		// 销售价--已分为单位
    	$request->input('spec')				== null || $data['spec'] 		= $request->input('spec');				// 规格
    	$request->input('stock')			== null || $data['stock']		= $request->input('stock');				// 库存
    	$request->input('unit')				== null || $data['unit'] 		= $request->input('unit');				// 计量单位
    	$request->input('introduce')		== null || $data['introduce']	= $request->input('introduce');			// 商品描述
		$data['shop_id'] = $shop_id;

		$re = Wares::where('id',$id)->update($data);

		if($re){

        	return self::success('更新成功');

        }else{

        	return self::error('更新失败');

        }	

    }

    /**
     * 商品上/下架(POST) api/wares/onoff
     * @param int $id 			商品id
     * @param int $delete 		上下架
     */
    public function onOff(Request $request)
    {
    	$id 	= $request->input('id');
    	$status = $request->input('status');

    	if(!$id || !$status){

    		return self::error('缺少参数');

    	}

    	$re = Wares::where('id',$id)->update(['status'=>$status]);

    	if($re){

        	return self::success('操作成功');

        }else{

        	return self::error('操作失败');

        }	

    }

    /**
     * 删除商品(POST) api/wares/delware
     * @param int $id 			商品id
     * @param int $delete 		删除
     */
    public function delWare(Request $request)
    {
    	$id 	= $request->input('id');
    	$delete = $request->input('delete');

    	if(!$id || !$delete){

    		return self::error('缺少参数');

    	}

    	$re = Wares::where('id',$id)->update(['delete'=>$delete]);

    	if($re){

        	return self::success('操作成功');

        }else{

        	return self::error('操作失败');

        }	

    }

    /**
     * 商品排序(POST) api/wares/chsort
     * @param int $id 			商品id
     * @param int $oldPostion 	更改前位置	
     * @param int $newPostion 	更改后位置
     */
    public function chSort(Request $request)
    {
    	$id 		= $request->input('id');
    	$oldPostion = $request->input('oldPostion');
    	$newPostion = $request->input('newPostion');

    	if($id<1 || $oldPostion<1 || $newPostion<1){

    		return self::error('缺少参数');

    	}

    	if ($oldPostion == $newPostion) {

            return self::error('无效的位置');

        } 

        // 商户SESSION数据
        $shopInfo = $request->session()->get('shopInfo');

        // 商户状态判断
        $shop_id = $shopInfo['id'];     // 店铺id

        if(!$shop_id){

            return self::error('未登录');

        }

        // 商品信息
        $info = Wares::where('id',$id)->where('shop_id',$shop_id)->first();

        if (empty($info) || $info['displayorder'] != $oldPostion) {

            return self::error('位置信息有误');

        }

        // 开始排序
        // 下调
        if ($oldPostion > $newPostion) {

            $endNum = $oldPostion - 1;

            $res = Wares::whereBetween('displayorder', [$newPostion, $endNum])
                    ->where('shop_id',$shop_id)
                    ->increment('displayorder');
        }
        // 上调
        if ($oldPostion < $newPostion) {

            $beginNum = $oldPostion + 1;

            $res = Wares::whereBetween('displayorder', [$beginNum, $newPostion])
                    ->where('shop_id',$shop_id)
                    ->decrement('displayorder');
        }

        $re = Wares::where('id',$id)->update(['displayorder' => $newPostion]);

        if($re){

            return self::success('操作成功');

        }else{

            return self::error('操作失败');
            
        }

    }


}